package com.twang.stock.ebay;

import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import com.twang.stock.entity.BaseCommon;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.service.jinyan.CommonService;
import com.twang.stock.threadPool.AsycTaskExecutorConfig;
import com.twang.stock.utils.http.HttpStockUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.assertj.core.util.Lists;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Slf4j
@Service
public class EbaySearchByTypeService {

    @Autowired
    private AsycTaskExecutorConfig asycTaskExecutorConfig;

    private static  String key_="ebay:EbaySearchByTypeService:";


    @Autowired
    private static RedisUtil redisUtil;

    public void getIndexPage(String urlPage) throws InterruptedException {
        List<Map<String, Object>> mapList = Lists.newArrayList();
       // for (int i = 1; i <= 10; i++) {
          //  int finalI = i;
          //  asycTaskExecutorConfig.taskQueueExecutor().execute(() -> {
           //     try {
                    eachpage(urlPage, mapList);
                    log.info(">>>>>>>>>> 采集页面结果：{}", JSONObject.toJSONString(mapList));
//           //     } catch (Exception e) {
//                    e.printStackTrace();
//                } finally {
//                    log.info("getIndexPage 剩余条数:{}", latch.getCount());
//                    latch.countDown();
//                }
//            });
        //}
      //  latch.await();
        log.info(">>>>>>>>>> 采集全部结果end ：{}", JSONObject.toJSONString(mapList));
    }


    /**
     * 查询个股热点
     *
     * @return
     */
    public  static String getPage(String url) throws Exception {
        String result = HttpStockUtils.getHtmlStrByUrlUtf8(url);
        String diff = JsonPath.read(result, "$.data.labels").toString();
//        List<ThsGeGuReZDian> list = JSONObject.parseArray(diff, ThsGeGuReZDian.class);
//        List<ThsGeGuReZDian> newList = Lists.newArrayList();
//        for (int i = list.size() - 1; i >= 0; i--) {
//            newList.add(list.get(i));
//        }
        return "";
    }



    public static void main(String[] args) throws Exception {
        List<Map<String,Object>> mapList= Lists.newArrayList();
        for(int i=1;i<=10;i++) {
            String page = "&_pgn=" + i;
            String url = "https://www.ebay.com/sch/i.html?_from=R40&_trksid=p4432023.m570.l1313&_nkw=sport+from+china&_sacat=0";
            String htmlResult = getPage111(url);
            // 网页内容解析
            Document doc = Jsoup.parse(htmlResult);
            // 获取所有 div 元素
            Elements divs = doc.select("div.s-item__image");
            // 遍历每个 div 元素
            for (Element div : divs) {
                // 在当前 div 中选择包含 href 属性的 a 元素
                Element link = div.select("a[href]").first();
                if (link != null) {
                    try {
                        Map<String, Object> userMap = new HashMap<>();

                        // 提取 href 属性的值
                        String href = link.attr("href");
                        // 访问主页，获取detail
                        itemUserHomePage(href, userMap, mapList);
                    } catch (Exception e) {
                        log.error(">>>>>>>> Exception ", e);
                    }
                } else {
                    System.out.println("No link found in this div");
                }
            }
            log.info(">>>>>>>>>> 采集结果：{}", JSONObject.toJSONString(mapList));
        }
    }

    private static boolean itemUserHomePage(String href, Map<String, Object> userMap, List<Map<String, Object>> mapList) {
        log.info(">>>>>>>>>>> 提取出了页面的商品地址 : {}", href);
        //1、访问连接 获取页面的商铺
        String indexResult = getPage111(href);
        Document   indexPagedoc_ = Jsoup.parse(indexResult);
        //获取feedback
        Elements elements = indexPagedoc_.select("div.x-sellercard-atf__info__about-seller span.ux-textspans--SECONDARY");
        if (!elements.isEmpty()) {
            try{
                String text = elements.get(0).text();
                String number = text.replaceAll("[()]", "");
                    // 去掉括号
                    log.info("提取的评分是:{} " + number); // 输出: 提取的数字是: 4549
                    userMap.put("评分",number);
            }catch (Exception e){
                log.error(">>>> 提取的评分 出错 ");
            }

        }

        //获取到用户的主页
        String userHomePage = indexPagedoc_.select("div.x-sellercard-atf__info__about-seller a").attr("href");
        if (StringUtils.isEmpty(userHomePage)) {
            return true;
        }

        Boolean istrue = Boolean.FALSE;
        userMap.put("主页地址", userHomePage);


        //2、获取about页面
        String aboutUrl = new StringBuffer().append(userHomePage.split("\\?")[0]).append("?_tab=about").toString();
        String aboutUrlResult = getPage111(aboutUrl);
        Document aboutDoc = Jsoup.parse(aboutUrlResult);
        Elements aboutDivs = aboutDoc.select("div.str-seller-card__store-stats-content");
        istrue = 获取好评率and已售出数量(aboutDoc, userMap);

        Elements locationDoc = aboutDoc.select("section.str-about-description__seller-info");
        // 提取所在地和注册时间
        istrue = loacationAndregistTime(locationDoc, userMap);
        if (istrue) {
            mapList.add(userMap);
        }else{
            String a=aboutUrl.replace("sch","usr").replace("/m.html","");
            String aboutUrlResult_ = getPage111(a);
            Document aboutDoc_ = Jsoup.parse(aboutUrlResult_);
            Elements locationDoc_ = aboutDoc_.select("section.str-about-description__seller-info");
            // 提取所在地和注册时间
            istrue = loacationAndregistTime(locationDoc_, userMap);
            mapList.add(userMap);
        }


        //2、获取页面

        //TODO 1、开始浏览主页的商品 以及销量
        // String userHomePageGoodsList = getPage111(href);
        //Document docUserHomePageGoodsList = Jsoup.parse(userHomePageGoodsList);
        //TODO 2、获取用户的地址，about页面

        //TODO 3、
        return false;
    }


    public  void queryPage(String  url) throws Exception {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        for (int i = 1; i <= 10; i++) {
            eachpage(url, mapList);
            log.info(">>>>>>>>>> 采集结果：{}", JSONObject.toJSONString(mapList));
        }
    }

    /**
     * 解析每个页面的数据
     * @param url
     * @param
     * @param mapList
     * @return
     */
    private  String eachpage(String url, List<Map<String, Object>> mapList) {
        try {
           // String page = "&_pgn=" + i;
           String urlpage = url;
            String htmlResult = getPage111(urlpage);
            // 网页内容解析
            Document doc = Jsoup.parse(htmlResult);
            // 获取所有 div 元素
            Elements divs = doc.select("div.s-item__image");
            // 遍历每个 div 元素
            for (Element div : divs) {
                asycTaskExecutorConfig.taskQueueExecutor().execute(() -> {
                    try{
                        // 在当前 div 中选择包含 href 属性的 a 元素
                        eachItemList(div, mapList);
                    }catch (Exception e) {
                        log.error(">>>>>>>>> 遍历商品 error:{}", e);
                    }
                });
            }
        } catch (Exception e) {
            log.info(">>>>>>>>>> 采集页面结果出错：{}", JSONObject.toJSONString(mapList));
        }
        return url;
    }


    /**
     * 解析每个页面的商品数据
     *
     * @param div
     * @param mapList
     * @return
     */
    private  boolean eachItemList(Element div, List<Map<String, Object>> mapList) {
        Document doc;
        String htmlResult;
        Element link = div.select("a[href]").first();
        if (link != null) {
            try {
                // 提取 href 属性的值
                String href = link.attr("href");
                log.info(">>>>>>>>>>> 提取出了页面的商品地址 : {}", href);
                //1、访问连接 获取页面的商铺
                htmlResult = getPage111(href);
                doc = Jsoup.parse(htmlResult);
                //获取到用户的主页
                String userHomePage = doc.select("div.x-sellercard-atf__info__about-seller a").attr("href");
                if (StringUtils.isEmpty(userHomePage)) {
                    return true;
                }
                Boolean istrue = Boolean.FALSE;
                Map<String, Object> userMap = new HashMap<>();
                userMap.put("主页地址", userHomePage);
                //2、获取about页面
                String aboutUrl = new StringBuffer().append(userHomePage.split("\\?")[0]).append("?_tab=about").toString();
                String aboutUrlResult = getHtmlStrByUrlDfcf(aboutUrl);
                Document aboutDoc = Jsoup.parse(aboutUrlResult);
                Elements aboutDivs = aboutDoc.select("div.str-seller-card__store-stats-content");
                istrue = 获取好评率and已售出数量(aboutDoc, userMap);

                Elements locationDoc = aboutDoc.select("section.str-about-description__seller-info");
                // 提取所在地和注册时间
                istrue = loacationAndregistTime(locationDoc, userMap);
                if (istrue) {
                    mapList.add(userMap);
                    redisUtil.addToList(key_,mapList);
                }
                //2、获取页面
                //TODO 1、开始浏览主页的商品 以及销量
                // String userHomePageGoodsList = getPage111(href);
                //Document docUserHomePageGoodsList = Jsoup.parse(userHomePageGoodsList);
                //TODO 2、获取用户的地址，about页面

                //TODO 3、
            } catch (Exception e) {
                log.error(">>>>>>>> Exception ", e);
            }
        } else {
            System.out.println("No link found in this div");
        }
        return false;
    }

    private static Boolean loacationAndregistTime(Elements locationDoc,Map userMap) {
        // 查找所有的 span 元素
        Elements spans = locationDoc.select("section.str-about-description__seller-info span");
        Boolean isTrue = Boolean.FALSE;
        Boolean locationFlag = Boolean.FALSE;
        // 由于信息是顺序排列的，所以可以通过索引直接访问
        if (spans.size() > 0) {
            // 所在地
            Element locationSpan = spans.get(0);
            String location = locationSpan.select("span.BOLD").text();
            Element registrationDateSpan = spans.get(3);
            String registrationDate = registrationDateSpan.select("span.BOLD").text();
            System.out.println("所在地: " + location);
            userMap.put("所在地", location);
            if (StringUtils.isNotBlank(location)) {
                locationFlag = Boolean.TRUE;
            }
            System.out.println("注册时间: " + registrationDate);
            userMap.put("注册时间", registrationDate);
            if (StringUtils.isNotBlank(registrationDate)) {
                locationFlag = Boolean.TRUE;
            }
        } else {
            System.out.println("所在地信息未找到");
        }
        return locationFlag;
    }

    private static Boolean 获取好评率and已售出数量(Document aboutDoc,Map userMap) {
        Boolean isTrue=Boolean.FALSE;

        // 使用 select 获取所有含有 title 属性的 div 元素
        Elements divsWithTitle = aboutDoc.select("div[title]");

        if (divsWithTitle.size() > 0) {
            // 提取第一个 div 的 title 属性
            Element firstDiv = divsWithTitle.get(0);
            String firstTitle = firstDiv.attr("title");
            System.out.println("First div title: " + firstTitle);
            userMap.put("好评率",firstTitle);

        } else {
            System.out.println("No div with title found");
        }

        if (divsWithTitle.size() > 1) {
            // 提取第二个 div 的 title 属性
            Element secondDiv = divsWithTitle.get(1);
            String secondTitle = secondDiv.attr("title");
            System.out.println("Second div title: " + secondTitle);
            userMap.put("出售商品数量",secondTitle);
        } else {
            System.out.println("Less than two divs with title found");
        }
        return Boolean.TRUE;
    }

    public static String getPage111(String urlString) {
        HttpURLConnection connection = null;

        try {
            // 创建 URL 对象
            URL url = new URL(urlString);

            // 打开连接
            connection.setRequestProperty("Accept-Charset", "UTF-8"); // 设置请求编码
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("Accept-Charset", "UTF-8"); // 设置请求编码
            // 设置请求方法
            connection.setRequestMethod("GET");

            // 设置请求头 (可选)
            // connection.setRequestProperty("Accept", "application/json");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);
            String contentType = connection.getContentType();
            System.out.println("查看服务器返回的编码信息 Content-Type: " + contentType); // 查看服务器返回的编码信息

            // 读取响应内容
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
            String inputLine;
            StringBuilder response = new StringBuilder();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // 打印响应内容
            System.out.println("Response Content: " + response.toString());
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return "";
    }


    public static String getHtmlStrByUrlDfcf(String url) throws Exception {
        // HttpClient 超时配置
        RequestConfig globalConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD)
                .setConnectionRequestTimeout(6000).setConnectTimeout(6000).build();
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(globalConfig).build();
        // HttpClient 超时配置
        HttpGet httpGetForFName = new HttpGet(url);
        // httpGetForFName.addHeader("User-Agent", USER_AGENT);
        // httpGetForFName.addHeader("Cookie", getCOOKIE());
        httpGetForFName.addHeader("Content-Type", "text/html; charset=utf-8");
        httpGetForFName.addHeader("Accept-Encoding", "gzip, deflate");
        httpGetForFName.addHeader("Connection", "keep-alive");
//        httpGetForFName.addHeader("Referer", "https://q.10jqka.com.cn/");
//        httpGetForFName.addHeader("Host", "q.10jqka.com.cn");

        // 发送请求，并执行
        CloseableHttpResponse response;
        String html = "";
        try {
            response = httpClient.execute(httpGetForFName);
            // response植入cookie
            Header[] ssoResponseHeader = response.getHeaders("Set-Cookie");
            InputStream in = response.getEntity().getContent();
            html = convertStreamToString(in);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return html;
    }

    public static String convertStreamToString(InputStream in) {
        StringBuilder sb = new StringBuilder();
        try {

            InputStreamReader isr = new InputStreamReader(in, "gbk");
            BufferedReader reader = new BufferedReader(isr);
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "/n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();

    }






}
